#!/bin/bash

KUBE_HOOKS_DIR="$(dirname "$(test -L "$0" && echo "$(dirname $0)/$(readlink "$0")" || echo "$0")")"

files_need_gofmt=()
files_need_boilerplate=()
files_need_description=()
for file in $(git diff --cached --name-only --diff-filter ACM | grep "\.go" | grep -v -e "third_party" -e "Godeps"); do
  # Check for files that fail gofmt.
  diff="$(git show ":${file}" | gofmt -s -d)"
  if [[ -n "$diff" ]]; then
    files_need_gofmt+=("${file}")
  fi

  # Check for files without the required boilerplate.
  boilerplate=$("${KUBE_HOOKS_DIR}/boilerplate.sh" "${file}")
  if [[ "$boilerplate" -eq "0" ]]; then
    files_need_boilerplate+=("${file}")
  fi
done

# Check sh files for boilerplate
for file in $(git diff --cached --name-only --diff-filter ACM | grep "\.sh" | grep -v -e "third_party" -e "Godeps"); do
  # Check for files without the required boilerplate.
  boilerplate=$("${KUBE_HOOKS_DIR}/boilerplate.sh" "${file}")
  if [[ "$boilerplate" -eq "0" ]]; then
    files_need_boilerplate+=("${file}")
  fi
done

# Check API schema definitions for field descriptions
# TODO: Check v1beta3, once it is documented
for file in $(git diff --cached --name-only --diff-filter ACM | egrep "pkg/api/v.[^/]*/types\.go" | grep -v "third_party" | grep -v v1beta3); do
  # Check for files with fields without description tags
  descriptionless=$("${KUBE_HOOKS_DIR}/description.sh" "${file}")
  if [[ "$descriptionless" -eq "0" ]]; then
    files_need_description+=("${file}")
  fi
done

if [[ "${#files_need_gofmt[@]}" -ne 0 ]]; then
  (
    echo
    echo "# *** ERROR: *** Some files have not been gofmt'd.  To fix these"
    echo "# errors, run gofmt -s -w <file>, or cut and paste the following:"
    echo "#   gofmt -s -w ${files_need_gofmt[@]}"
    echo "#"
    echo "# Your commit will be aborted unless you override this warning. To"
    echo "# commit in spite of these format errors, delete the following line:"
    echo "#   COMMIT_BLOCKED_ON_GOFMT"
  ) >> $1
fi

if [[ "${#files_need_boilerplate[@]}" -ne 0 ]]; then
  (
    echo
    echo "# *** ERROR: *** Some files are missing the required boilerplate"
    echo "# header from hooks/boilerplate.txt:"
    for file in "${files_need_boilerplate[@]}"; do
      echo "#   ${file}"
    done
    echo "#"
    echo "# Your commit will be aborted unless you fix these."
    echo "#   COMMIT_BLOCKED_ON_BOILERPLATE"
    echo
  ) >> $1
fi

if [[ "${#files_need_description[@]}" -ne 0 ]]; then
  (
    echo
    echo "# *** ERROR: *** Some API files are missing the required field descriptions"
    echo "# Add description tags to all non-inline fields in the following files:"
    for file in "${files_need_description[@]}"; do
      echo "#   ${file}"
    done
    echo "#"
    echo "# Your commit will be aborted unless you fix these."
    echo "#   COMMIT_BLOCKED_ON_DESCRIPTION"
    echo
  ) >> $1
fi

if ! hack/verify-gendocs.sh > /dev/null; then
  (
    echo
    echo "# *** ERROR: *** docs are out of sync between cli and markdown"
    echo "# run hack/run-gendocs.sh > docs/kubectl.md to regenerate"
    echo
    echo "#"
    echo "# Your commit will be aborted unless you regenerate docs."
    echo "    COMMIT_BLOCKED_ON_GENDOCS"
    echo
  ) >> $1
fi
